package com.hadwinling.control;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.hadwinling.entity.ForumOfficalarticle;
import com.hadwinling.entity.ForumPost;
import com.hadwinling.entity.ForumTopic;
import com.hadwinling.entity.ForumUser;
import com.hadwinling.service.ArticleService;
import com.hadwinling.service.OfficalArticleService;
import com.hadwinling.service.PostService;
import com.hadwinling.service.TopicService;

@Controller
public class fatieControl {
	@Autowired
	private ArticleService articleService;
	@Autowired
	private TopicService topicService;
	@Autowired
	private PostService postService;
	@Autowired
	private OfficalArticleService officalArticleService;

	@RequestMapping(value = "/user/fatie.action")
	public void fatie(HttpServletRequest request, HttpServletResponse response) throws IOException {
		if (request.getSession().getAttribute("user") == null) {
			response.sendRedirect(request.getContextPath() + "/jsp/loginregister.jsp");
		} else {
			response.sendRedirect(request.getContextPath() + "/jsp/fatie.jsp");
		}
	}

	@RequestMapping(value = "user/newpost.action", method = RequestMethod.POST)
	public void newPost(int topicid, ForumPost post, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		ForumTopic topic = topicService.selectTopicByTid(topicid);
		ForumPost forumPost = post;
		forumPost.setPcontent(forumPost.getPcontent().replaceAll("(\r\n|\r|\n|\n\r)", "<br>"));
		forumPost.setUser((ForumUser) request.getSession().getAttribute("user"));
		forumPost.setCreatetime(new Date());
		forumPost.setTopic(topic);
		if (forumPost.getTopic().getTid() == 7) {
			forumPost.setPcontent(forumPost.getPcontent() + "<span style=\'position: relative;"
					+ "top: 0px;left:100px ;font-size: 30px;z-index: -1;color:#c0c0c0\'>" + "代购区所有交易均由用户自己负责</span>");
		}
		if (postService.postNewPost(forumPost)) {
			articleService.fatiezijiawufen(post.getUser().getUid());
			response.sendRedirect(request.getContextPath() + "/luntan/luntanshouye.action?tid=1");
		} else {
			System.out.println("发帖失败");
		}
	}

	@RequestMapping(value = "/admin/newoa.action", method = RequestMethod.POST)
	public void newOa(ForumOfficalarticle forumOfficalarticle, HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if (officalArticleService.insertArticle(forumOfficalarticle)) {
			request.setAttribute("info", 1);
//            response.sendRedirect(request.getContextPath() + "/admin/wenzhangfabiao.jsp?info=1");
			request.getRequestDispatcher("/admin/wenzhangfabiao.jsp").forward(request, response);
		}
	}

	@RequestMapping(value = "/user/updatePost.action", method = RequestMethod.POST)
	public void updatePost(ForumPost post, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		ForumPost forumPost = post;
		forumPost.setUser((ForumUser) request.getSession().getAttribute("user"));
		forumPost.setCreatetime(new Date());
		if (postService.updatePost(forumPost)) {
			response.sendRedirect(request.getContextPath() + "/user/userCenter.action");
		}
	}

	@RequestMapping(value = "/admin/newArticle.action")
	public void newArticle(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getRequestDispatcher("/admin/wenzhangfabiao.jsp").forward(request, response);
	}

	@RequestMapping(value = "/user/picload.action", method = RequestMethod.POST)
	public @ResponseBody Map<String, String> upload(MultipartFile uploadImage, HttpServletRequest request)
			throws IOException {
		// 原始文件名称
		String pictureFile_name = uploadImage.getOriginalFilename();
		// 新文件名称
		String newFileName = UUID.randomUUID().toString()
				+ pictureFile_name.substring(pictureFile_name.lastIndexOf("."));

		// 获得图片上传的路径
		String path = request.getServletContext().getRealPath("image/upload");

		// 上传图片
		File uploadPic = new File(path + "/" + newFileName);

		if (!uploadPic.exists()) {
			uploadPic.mkdirs();
		}
		// 向磁盘写文件
		uploadImage.transferTo(uploadPic);
		System.out.println(request.getServletContext().getContextPath() + "image/upload" + "/" + newFileName);
		Map<String, String> map = new HashMap();
		map.put("url", request.getContextPath() + "/image/upload" + "/" + newFileName);
		return map;

	}

	@RequestMapping(value = "/admin/oapicload.action", method = RequestMethod.POST)
	public @ResponseBody Map<String, String> oaupload(MultipartFile oauploadImage, HttpServletRequest request)
			throws IOException {
		// 原始文件名称
		String pictureFile_name = oauploadImage.getOriginalFilename();
		// 新文件名称
		String newFileName = UUID.randomUUID().toString()
				+ pictureFile_name.substring(pictureFile_name.lastIndexOf("."));

		// 获得图片上传的路径
		String path = request.getServletContext().getRealPath("image/oaupload");

		// 上传图片
		File uploadPic = new File(path + "/" + newFileName);

		if (!uploadPic.exists()) {
			uploadPic.mkdirs();
		}
		// 向磁盘写文件
		oauploadImage.transferTo(uploadPic);
		System.out.println(request.getServletContext().getContextPath() + "image/oaupload" + "/" + newFileName);
		Map<String, String> map = new HashMap();
		map.put("url", request.getContextPath() + "/image/oaupload" + "/" + newFileName);
		return map;

	}

	@RequestMapping(value = "/admin/articleList.action")
	public void articleList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setAttribute("articleList", articleService.getAllOfficalArticles());
		request.getRequestDispatcher("/admin/wenzhangxiangqing.jsp").forward(request, response);
	}

	@RequestMapping(value = "/admin/getArticle.action", method = RequestMethod.POST)
	public @ResponseBody ForumOfficalarticle findArticleByOaid(String oaid, HttpServletRequest request,
			HttpServletResponse response) {
		Integer id = Integer.parseInt(oaid);
		return articleService.selectOfficalArticleByOaid(id);
	}

	@RequestMapping(value = "/admin/deleteArticle.action", method = RequestMethod.POST)
	public @ResponseBody String deleteArticle(String oaid, HttpServletRequest request, HttpServletResponse response) {
		if (officalArticleService.deleteArticle(Integer.parseInt(oaid))) {
			return "1";
		} else {
			return "0";
		}

	}
}
